package com.sg.service.biz.pbff.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.pbff.req.*;
import com.sg.dto.biz.pbff.res.*;
import com.sg.service.biz.pbff.BehaviorTypeService;
import com.wicket.okrframework.integration.AppSceneService;
import com.wicket.okrframework.integration.BehaviorTypeService;
import com.wicket.okrframework.integration.InductionRecordService;
import com.wicket.okrframework.integration.dto.*;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class BehaviorTypeServiceImpl implements BehaviorTypeService {

    @Resource
    com.sg.service.biz.framework.BehaviorTypeService fwBaseBehaviorTypeClient;
    @Resource
    com.sg.service.biz.framework.InductionRecordService fwBaseInductionRecordClient;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    com.sg.service.biz.framework.AppSceneService fwBaseAppSceneClient;

    /**
     * B1-1查询行为类型列表（平台）[771]
     * gen by moon at 10/23/2022, 9:18:46 PM
     */
    @Trace(operationName = "B1-1查询行为类型列表（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryBehaviorTypeListRespDto queryBehaviorTypeList(BffQueryBehaviorTypeListReqDto reqDto) {


        QueryBehaviorTypeListRespDto queryBehaviorTypeListRespDto_1 = null;
        BatchQueryInductionRecordListComRespDto batchQueryInductionRecordListComRespDto_1 = null;
//步骤0: D1-1查询行为类型列表 - queryBehaviorTypeList
        QueryBehaviorTypeListRespDto queryBehaviorTypeListRespDto = null;
        QueryBehaviorTypeListReqDto queryBehaviorTypeListReqDto = new QueryBehaviorTypeListReqDto();
        if (reqDto != null) {
            queryBehaviorTypeListReqDto.setBizAppId(reqDto.getBizAppId());//SimpleFieldAssign//sourceId:387995_1
            queryBehaviorTypeListReqDto.setBizSceneId(reqDto.getBizSceneId());//SimpleFieldAssign//sourceId:387996_1
            queryBehaviorTypeListReqDto.setBehaviorTypeName(reqDto.getBehaviorTypeName());//SimpleFieldAssign//sourceId:27922_1
            queryBehaviorTypeListReqDto.setIsArchive(reqDto.getIsArchive());//SimpleFieldAssign//sourceId:27924_1
        }

        /*D1-1查询行为类型列表[719]   */
        Assert.isNull(queryBehaviorTypeListReqDto.getIsArchive(), "B1-1查询行为类型列表（平台）-D1-1查询行为类型列表-是否存档不能为空", false);
        queryBehaviorTypeListRespDto = fwBaseBehaviorTypeClient.queryBehaviorTypeList(queryBehaviorTypeListReqDto).getData();


        queryBehaviorTypeListRespDto_1 = queryBehaviorTypeListRespDto;

//步骤1: D1-3批量查询就职记录列表（公共） - batchQueryInductionRecordListCom
        BatchQueryInductionRecordListComRespDto batchQueryInductionRecordListComRespDto = null;
        if (queryBehaviorTypeListRespDto != null) {
            BatchQueryInductionRecordListComReqDto batchQueryInductionRecordListComReqDto = new BatchQueryInductionRecordListComReqDto();
            if (queryBehaviorTypeListRespDto != null) {
                batchQueryInductionRecordListComReqDto.setPerOrgInductionRecordList(queryBehaviorTypeListRespDto.getBehaviorTypeList().stream().map(item -> item.getOperationInductionId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:34624_1
            }

            /*D1-3批量查询就职记录列表（公共）[602]   */

            batchQueryInductionRecordListComRespDto = fwBaseInductionRecordClient.batchQueryInductionRecordListCom(batchQueryInductionRecordListComReqDto).getData();


            batchQueryInductionRecordListComRespDto_1 = batchQueryInductionRecordListComRespDto;
        }

        BffQueryBehaviorTypeListRespDto retData = new BffQueryBehaviorTypeListRespDto();
        //数据集融合  MoreListToOneSource
        if (queryBehaviorTypeListRespDto_1 != null && !CollectionUtil.isEmpty(queryBehaviorTypeListRespDto_1.getBehaviorTypeList())) {
            for (BehaviorTypeDto behaviorTypeDto : queryBehaviorTypeListRespDto_1.getBehaviorTypeList()) {
                BffBehaviorTypeDto retElm = new BffBehaviorTypeDto();
                if (queryBehaviorTypeListRespDto_1 != null) {
                    retElm.setBehaviorTypeId(behaviorTypeDto.getBehaviorTypeId());//SimpleFieldAssign//sourceId:11962_2
                    retElm.setBehaviorTypeCode(behaviorTypeDto.getBehaviorTypeCode());//SimpleFieldAssign//sourceId:11963_2
                    retElm.setBehaviorTypeName(behaviorTypeDto.getBehaviorTypeName());//SimpleFieldAssign//sourceId:11964_2
                    retElm.setBizAppId(behaviorTypeDto.getBizAppId());//SimpleFieldAssign//sourceId:15626_2
                    retElm.setBizAppCode(behaviorTypeDto.getBizAppCode());//SimpleFieldAssign//sourceId:15627_2
                    retElm.setBizAppName(behaviorTypeDto.getBizAppName());//SimpleFieldAssign//sourceId:15628_2
                    retElm.setSubjectLifeCycle(behaviorTypeDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:11967_2
                    retElm.setOperationInductionId(behaviorTypeDto.getOperationInductionId());//SimpleFieldAssign//sourceId:11968_2
                    retElm.setOperateTime(behaviorTypeDto.getOperateTime());//SimpleFieldAssign//sourceId:11969_2
                }
                retData.getBehaviorTypeList().add(retElm);
                if (batchQueryInductionRecordListComRespDto_1 != null) {
                    for (PerOrgInductionRecordDto perOrgInductionRecordDto : batchQueryInductionRecordListComRespDto_1.getPerOrgInductionRecordList()) {
                        if (perOrgInductionRecordDto.getInductionRecordId().equals(behaviorTypeDto.getOperationInductionId())) {
                            if (batchQueryInductionRecordListComRespDto_1 != null) {
                                retElm.setUserId(perOrgInductionRecordDto.getUserId());//SimpleFieldAssign//sourceId:38817_2
                                retElm.setUserFace(perOrgInductionRecordDto.getUserFace());//SimpleFieldAssign//sourceId:38820_2
                                retElm.setIdCardName(perOrgInductionRecordDto.getIdCardName());//SimpleFieldAssign//sourceId:38818_2
                                retElm.setNickName(perOrgInductionRecordDto.getNickName());//SimpleFieldAssign//sourceId:38819_2
                            }
                        }
                    }
                }

            }
        }//sourceId:27932_1


        retData.setPageNum(queryBehaviorTypeListRespDto.getPageNum());
        retData.setPageSize(queryBehaviorTypeListRespDto.getPageSize());
        retData.setTotal(queryBehaviorTypeListRespDto.getTotalNum());
        retData.setIsLastPage((Math.ceil(Double.valueOf(queryBehaviorTypeListRespDto.getTotalNum()) / Double.valueOf(queryBehaviorTypeListRespDto.getPageSize()))) <= retData.getPageNum());


        return retData;
    }

    /**
     * B1-1修改行为类型（平台）[775]
     * gen by moon at 9/1/2022, 7:22:37 PM
     */
    @Trace(operationName = "B1-1修改行为类型（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffUpdateBehaviorTypeRespDto updateBehaviorType(BffUpdateBehaviorTypeReqDto reqDto) {


        //步骤0: D1-1修改行为类型 - updateBehaviorType
        UpdateBehaviorTypeRespDto updateBehaviorTypeRespDto = null;
        UpdateBehaviorTypeReqDto updateBehaviorTypeReqDto = new UpdateBehaviorTypeReqDto();
        if (reqDto != null) {
            updateBehaviorTypeReqDto.setBehaviorTypeId(reqDto.getBehaviorTypeId());//sourceId:28044_1
            updateBehaviorTypeReqDto.setBehaviorTypeCode(reqDto.getBehaviorTypeCode());//sourceId:28045_1
            updateBehaviorTypeReqDto.setBehaviorTypeName(reqDto.getBehaviorTypeName());//sourceId:28046_1
            updateBehaviorTypeReqDto.setBehaviorTypeDes(reqDto.getBehaviorTypeDes());//sourceId:28047_1
            updateBehaviorTypeReqDto.setBizAppId(reqDto.getBizAppId());//sourceId:28048_1
            updateBehaviorTypeReqDto.setBizSceneId(reqDto.getBizSceneId());//sourceId:286056_1
            updateBehaviorTypeReqDto.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:28049_1
        }

        /*D1-1修改行为类型[718]   */
        Assert.isNull(updateBehaviorTypeReqDto.getBehaviorTypeId(), "B1-1修改行为类型（平台）-D1-1修改行为类型-行为类型ID不能为空", false);
        updateBehaviorTypeRespDto = fwBaseBehaviorTypeClient.updateBehaviorType(updateBehaviorTypeReqDto).getData();


        BffUpdateBehaviorTypeRespDto retData = new BffUpdateBehaviorTypeRespDto();


        return retData;
    }

    /**
     * B1-1新增行为类型（平台）[773]
     * gen by moon at 9/1/2022, 7:22:36 PM
     */
    @Trace(operationName = "B1-1新增行为类型（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffAddBehaviorTypeRespDto addBehaviorType(BffAddBehaviorTypeReqDto reqDto) {


        AddBehaviorTypeRespDto addBehaviorTypeRespDto_1 = null;
//步骤0: D1-1新增行为类型 - addBehaviorType
        AddBehaviorTypeRespDto addBehaviorTypeRespDto = null;
        AddBehaviorTypeReqDto addBehaviorTypeReqDto = new AddBehaviorTypeReqDto();
        if (reqDto != null) {
            addBehaviorTypeReqDto.setBehaviorTypeCode(reqDto.getBehaviorTypeCode());//sourceId:28015_1
            addBehaviorTypeReqDto.setBehaviorTypeName(reqDto.getBehaviorTypeName());//sourceId:28016_1
            addBehaviorTypeReqDto.setBehaviorTypeDes(reqDto.getBehaviorTypeDes());//sourceId:28017_1
            addBehaviorTypeReqDto.setBizAppId(reqDto.getBizAppId());//sourceId:28018_1
            addBehaviorTypeReqDto.setBizSceneId(reqDto.getBizSceneId());//sourceId:286051_1
            addBehaviorTypeReqDto.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:28019_1
        }

        /*D1-1新增行为类型[716]   */
        Assert.isNull(addBehaviorTypeReqDto.getBehaviorTypeCode(), "B1-1新增行为类型（平台）-D1-1新增行为类型-行为类型编码不能为空", false);
        Assert.isNull(addBehaviorTypeReqDto.getBehaviorTypeName(), "B1-1新增行为类型（平台）-D1-1新增行为类型-行为类型名称不能为空", false);
        Assert.isNull(addBehaviorTypeReqDto.getBizAppId(), "B1-1新增行为类型（平台）-D1-1新增行为类型-冗余业务应用ID不能为空", false);
        Assert.isNull(addBehaviorTypeReqDto.getBizSceneId(), "B1-1新增行为类型（平台）-D1-1新增行为类型-关联业务场景ID不能为空", false);
        Assert.isNull(addBehaviorTypeReqDto.getSubjectLifeCycle(), "B1-1新增行为类型（平台）-D1-1新增行为类型-主体生命周期不能为空", false);
        addBehaviorTypeRespDto = fwBaseBehaviorTypeClient.addBehaviorType(addBehaviorTypeReqDto).getData();


        addBehaviorTypeRespDto_1 = addBehaviorTypeRespDto;

        BffAddBehaviorTypeRespDto retData = new BffAddBehaviorTypeRespDto();
        if (addBehaviorTypeRespDto_1 != null) {
            retData.setBehaviorTypeId(addBehaviorTypeRespDto_1.getBehaviorTypeId());//sourceId:28026_1
        }


        return retData;
    }

    /**
     * B1-1查询行为类型详情（平台）[772]
     * gen by moon at 10/28/2022, 8:54:28 PM
     */
    @Trace(operationName = "B1-1查询行为类型详情（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryBehaviorTypeDetailRespDto queryBehaviorTypeDetail(BffQueryBehaviorTypeDetailReqDto reqDto) {


        QueryBehaviorTypeDetailRespDto queryBehaviorTypeDetailRespDto_1 = null;
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto_1 = null;
//步骤0: D1-1查询行为类型详情 - queryBehaviorTypeDetail
        QueryBehaviorTypeDetailRespDto queryBehaviorTypeDetailRespDto = null;
        QueryBehaviorTypeDetailReqDto queryBehaviorTypeDetailReqDto = new QueryBehaviorTypeDetailReqDto();
        if (reqDto != null) {
            queryBehaviorTypeDetailReqDto.setBehaviorTypeId(reqDto.getBehaviorTypeId());//SimpleFieldAssign//sourceId:27972_1
        }

        /*D1-1查询行为类型详情[720]   */
        Assert.isNull(queryBehaviorTypeDetailReqDto.getBehaviorTypeId(), "B1-1查询行为类型详情（平台）-D1-1查询行为类型详情-行为类型ID不能为空", false);
        queryBehaviorTypeDetailRespDto = fwBaseBehaviorTypeClient.queryBehaviorTypeDetail(queryBehaviorTypeDetailReqDto).getData();
        Assert.isTrue(queryBehaviorTypeDetailRespDto == null || queryBehaviorTypeDetailRespDto.getBehaviorTypeId() == null, "返回值不能为空", false);

        queryBehaviorTypeDetailRespDto_1 = queryBehaviorTypeDetailRespDto;

//步骤1: D1-3查询个人就职记录信息详情（公共） - queryInductionRecordInfoDetailCom
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto = null;
        if (queryBehaviorTypeDetailRespDto != null) {
            QueryInductionRecordInfoDetailComReqDto queryInductionRecordInfoDetailComReqDto = new QueryInductionRecordInfoDetailComReqDto();
            if (queryBehaviorTypeDetailRespDto != null) {
                queryInductionRecordInfoDetailComReqDto.setInductionRecordId(queryBehaviorTypeDetailRespDto.getOperationInductionId());//SimpleFieldAssign//sourceId:27944_1
            }

            /*D1-3查询个人就职记录信息详情（公共）[715]   */
            Assert.isNull(queryInductionRecordInfoDetailComReqDto.getInductionRecordId(), "B1-1查询行为类型详情（平台）-D1-3查询个人就职记录信息详情（公共）-就职记录ID不能为空", false);
            queryInductionRecordInfoDetailComRespDto = fwBaseInductionRecordClient.queryInductionRecordInfoDetailCom(queryInductionRecordInfoDetailComReqDto).getData();


            queryInductionRecordInfoDetailComRespDto_1 = queryInductionRecordInfoDetailComRespDto;
        }

        BffQueryBehaviorTypeDetailRespDto retData = new BffQueryBehaviorTypeDetailRespDto();
        if (queryBehaviorTypeDetailRespDto_1 != null) {
            retData.setBehaviorTypeId(queryBehaviorTypeDetailRespDto_1.getBehaviorTypeId());//SimpleFieldAssign//sourceId:27982_1
            retData.setBehaviorTypeCode(queryBehaviorTypeDetailRespDto_1.getBehaviorTypeCode());//SimpleFieldAssign//sourceId:27983_1
            retData.setBehaviorTypeName(queryBehaviorTypeDetailRespDto_1.getBehaviorTypeName());//SimpleFieldAssign//sourceId:27984_1
            retData.setBehaviorTypeDes(queryBehaviorTypeDetailRespDto_1.getBehaviorTypeDes());//SimpleFieldAssign//sourceId:27985_1
            retData.setBizAppId(queryBehaviorTypeDetailRespDto_1.getBizAppId());//SimpleFieldAssign//sourceId:145040_1
            retData.setBizAppCode(queryBehaviorTypeDetailRespDto_1.getBizAppCode());//SimpleFieldAssign//sourceId:407748_1
            retData.setBizAppName(queryBehaviorTypeDetailRespDto_1.getBizAppName());//SimpleFieldAssign//sourceId:136356_1
            retData.setBizSceneId(queryBehaviorTypeDetailRespDto_1.getBizSceneId());//SimpleFieldAssign//sourceId:286085_1
            retData.setBizSceneCode(queryBehaviorTypeDetailRespDto_1.getBizSceneCode());//SimpleFieldAssign//sourceId:407749_1
            retData.setBizSceneName(queryBehaviorTypeDetailRespDto_1.getBizSceneName());//SimpleFieldAssign//sourceId:286086_1
            retData.setSubjectLifeCycle(queryBehaviorTypeDetailRespDto_1.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:27987_1
            retData.setOperationInductionId(queryBehaviorTypeDetailRespDto_1.getOperationInductionId());//SimpleFieldAssign//sourceId:27988_1
            retData.setOperateTime(queryBehaviorTypeDetailRespDto_1.getOperateTime());//SimpleFieldAssign//sourceId:27989_1
        }
        if (queryInductionRecordInfoDetailComRespDto_1 != null) {
            retData.setUserId(queryInductionRecordInfoDetailComRespDto_1.getUserId());//SimpleFieldAssign//sourceId:27992_1
            retData.setUserFace(queryInductionRecordInfoDetailComRespDto_1.getUserFace());//SimpleFieldAssign//sourceId:27995_1
            retData.setIdCardName(queryInductionRecordInfoDetailComRespDto_1.getIdCardName());//SimpleFieldAssign//sourceId:27993_1
            retData.setNickName(queryInductionRecordInfoDetailComRespDto_1.getNickName());//SimpleFieldAssign//sourceId:27994_1
        }


        return retData;
    }

    /**
     * B1查询赋能的行为类型列表(管理)(边界)[5002]
     * gen by moon at 10/25/2022, 10:01:17 AM
     */
    @Trace(operationName = "B1查询赋能的行为类型列表(管理)(边界)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryEnablingBehaviorTypeListBorderRespDto queryEnablingBehaviorTypeListBorder(BffQueryEnablingBehaviorTypeListBorderReqDto reqDto) {


        BatchQueryBehaviorTypeBorderRespDto batchQueryBehaviorTypeBorderRespDto_1 = null;
//步骤0: D3查询当前被赋能业务场景列表 - queryCurrentBizAppSceneRelEdList
        QueryCurrentBizAppSceneRelEdListRespDto queryCurrentBizAppSceneRelEdListRespDto = null;
        QueryCurrentBizAppSceneRelEdListReqDto queryCurrentBizAppSceneRelEdListReqDto = new QueryCurrentBizAppSceneRelEdListReqDto();
        if (reqDto != null) {
            queryCurrentBizAppSceneRelEdListReqDto.setByBizAppCode(reqDto.getByBizAppCode());//SimpleFieldAssign//sourceId:388631_1
            queryCurrentBizAppSceneRelEdListReqDto.setBizAppCode(reqDto.getBizAppCode());//SimpleFieldAssign//sourceId:388632_1
        }

        /*D3查询当前被赋能业务场景列表[5001]   */
        Assert.isNull(queryCurrentBizAppSceneRelEdListReqDto.getByBizAppCode(), "B1查询赋能的行为类型列表(管理)(边界)-D3查询当前被赋能业务场景列表-被赋能业务应用CODE不能为空", false);
        Assert.isNull(queryCurrentBizAppSceneRelEdListReqDto.getBizAppCode(), "B1查询赋能的行为类型列表(管理)(边界)-D3查询当前被赋能业务场景列表-赋能业务应用Code不能为空", false);
        queryCurrentBizAppSceneRelEdListRespDto = fwBaseAppSceneClient.queryCurrentBizAppSceneRelEdList(queryCurrentBizAppSceneRelEdListReqDto).getData();


//步骤1: D1-1批量查询行为类型(边界) - batchQueryBehaviorTypeBorder
        BatchQueryBehaviorTypeBorderRespDto batchQueryBehaviorTypeBorderRespDto = null;
        if (queryCurrentBizAppSceneRelEdListRespDto != null) {
            BatchQueryBehaviorTypeBorderReqDto batchQueryBehaviorTypeBorderReqDto = new BatchQueryBehaviorTypeBorderReqDto();
            if (queryCurrentBizAppSceneRelEdListRespDto != null) {
                batchQueryBehaviorTypeBorderReqDto.setBehaviorTypeList(queryCurrentBizAppSceneRelEdListRespDto.getBizAppSceneRelList().stream().map(item -> item.getBizSceneId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:388645_1
            }

            /*D1-1批量查询行为类型(边界)[5003]   */

            batchQueryBehaviorTypeBorderRespDto = fwBaseBehaviorTypeClient.batchQueryBehaviorTypeBorder(batchQueryBehaviorTypeBorderReqDto).getData();


            batchQueryBehaviorTypeBorderRespDto_1 = batchQueryBehaviorTypeBorderRespDto;
        }

        BffQueryEnablingBehaviorTypeListBorderRespDto retData = new BffQueryEnablingBehaviorTypeListBorderRespDto();
        if (batchQueryBehaviorTypeBorderRespDto_1 != null) {
            retData.setEnablingBehaviorTypeList(batchQueryBehaviorTypeBorderRespDto_1.getBehaviorTypeList().stream().map(item -> BeanUtil.toBean(item, BffEnablingBehaviorTypeDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:388647_1
        }


        return retData;
    }
    //
}
